package com.lanlian.cayman.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDelegate;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.lanlian.cayman.R;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.event.EventCode;
import com.lanlian.commonlib.event.EventMessage;
import com.lanlian.commonlib.util.EventBusUtils;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : xiaobo.hu@foxtry.com
 *     date   : 2018/8/13 14:10
 *     desc   : 设置
 *     version: 1.0
 * </pre>
 */
public class SettingActivity extends BaseActivity {

    private CheckBox mCbSecond;
    private RadioGroup mRgColor;
    private CheckBox mCbNight;
    private RelativeLayout mLaySecond;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting, true);
        initView();
        setUserInfo();
        setListeners();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("设置");
        mCbSecond = findViewById(R.id.cb_second);
        mRgColor = findViewById(R.id.rg_color);
        mCbNight = findViewById(R.id.cb_night);
        mLaySecond = findViewById(R.id.lay_second);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mCbSecond.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mDBManager.setSecondConfirm(isChecked);
            }
        });
        mCbNight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mDBManager.setTopicColor(isChecked ? 2 : 1);
                if (!isChecked) {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                } else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                }
                EventBusUtils.post(new EventMessage(EventCode.EVENT_COLOR_TOPIC_CHANGE));
            }
        });
        mRgColor.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_red_up:
                        mDBManager.setKLineColor(1);
                        EventBusUtils.post(new EventMessage(EventCode.EVENT_MARKET_COLOR_CHANGE));
                        break;
                    case R.id.rb_green_up:
                        mDBManager.setKLineColor(2);
                        EventBusUtils.post(new EventMessage(EventCode.EVENT_MARKET_COLOR_CHANGE));
                        break;
                    default:
                        break;
                }
            }
        });
    }

    /**
     * 设置用户信息
     */
    private void setUserInfo() {
        if (mDBManager.isLogined()) {
            mLaySecond.setVisibility(View.VISIBLE);
            mCbSecond.setChecked(mDBManager.getSecondConfirm());
        } else {
            mLaySecond.setVisibility(View.GONE);
        }
        mRgColor.check(mDBManager.getKLineColor() == 1 ? R.id.rb_red_up : R.id.rb_green_up);
        mCbNight.setChecked(mDBManager.getTopicColor() == 2);
    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    public void onReceiveEvent(EventMessage event) {
        super.onReceiveEvent(event);
        switch (event.getCode()) {
            case EventCode.EVENT_COLOR_TOPIC_CHANGE:
                if (mDBManager.getTopicColor() == 1) {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                } else if (mDBManager.getTopicColor() == 2) {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                }
                finish();
                startActivity(new Intent(this, this.getClass()));
                overridePendingTransition(0, 0);
                break;
            default:
                break;
        }
    }

}
